package com.jlf.core.server.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jlf.core.annotation.server.add.AddAfterProcessor;
import com.jlf.core.annotation.server.add.AddPostProcessor;
import com.jlf.core.annotation.server.add.AddPreProcessor;
import com.jlf.core.annotation.server.delete.DeleteAfterProcessor;
import com.jlf.core.annotation.server.delete.DeletePostProcessor;
import com.jlf.core.annotation.server.delete.DeletePreProcessor;
import com.jlf.core.annotation.server.edit.EditAfterProcessor;
import com.jlf.core.annotation.server.edit.EditPostProcessor;
import com.jlf.core.annotation.server.edit.EditPreProcessor;
import com.jlf.core.domain._SimpleDO;
import com.jlf.core.error.exception.AddObjectException;
import com.jlf.core.error.exception.ModifyObjectException;
import com.jlf.core.mp.persistence._SimplePersistence;
import com.jlf.core.server._SimpleServer;
import com.jlf.core.server.entity.*;
import com.jlf.core.server.functional.*;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * 简单服务的具体实现
 *
 * @author wujr
 * 2023/7/18
 * @version 1.0
 * 【变更历史】
 * [wujr 2023/7/18 1.0 新增]
 */
@Slf4j
public class _SimpleServerImpl<T extends _SimpleDO<T>> implements _SimpleServer<T> {
    /**
     * 该服务对应的领域的持久化bean
     */
    private final _SimplePersistence<T> _persistence;
    /**
     * 该服务对应的服务bean
     */
    private final Object _bean;
    private final Class<?> _class;
    protected final TreeMap<Integer, List<SimpleConsumer<T>>> _preAddConsumer = new TreeMap<>();
    protected final TreeMap<Integer, List<SimpleConsumer<AddRecordWrapper<T>>>> _preWrapperAddConsumer = new TreeMap<>();
    protected final TreeMap<Integer, List<SimpleResultConsumer<T>>> _afterAddConsumer = new TreeMap<>();
    protected final TreeMap<Integer, List<SimpleResultConsumer<AddRecordWrapper<T>>>> _afterWrapperAddConsumer = new TreeMap<>();
    protected final TreeMap<Integer, List<SimpleConsumer<T>>> _postAddConsumer = new TreeMap<>();
    protected final TreeMap<Integer, List<SimpleConsumer<AddRecordWrapper<T>>>> _postWrapperAddConsumer = new TreeMap<>();
    protected final TreeMap<Integer, List<SimpleConsumer<EditRecordWrapper<T>>>> _preWrapperEditConsumer = new TreeMap<>();
    protected final TreeMap<Integer, List<SimpleConsumer<EditRecordWrapper<T>>>> _postWrapperEditConsumer = new TreeMap<>();
    protected final TreeMap<Integer, List<SimpleResultConsumer<EditRecordWrapper<T>>>> _afterWrapperEditConsumer = new TreeMap<>();
    protected final TreeMap<Integer, List<SimpleConsumer2<T>>> _preEditConsumer = new TreeMap<>();
    protected final TreeMap<Integer, List<SimpleConsumer2<T>>> _postEditConsumer = new TreeMap<>();
    protected final TreeMap<Integer, List<SimpleResultConsumer2<T>>> _afterEditConsumer = new TreeMap<>();
    protected final TreeMap<Integer, List<SimpleConsumer<T>>> _preDeleteConsumer = new TreeMap<>();
    protected final TreeMap<Integer, List<SimpleConsumer<DeleteRecordWrapper<T>>>> _preWrapperDeleteConsumer = new TreeMap<>();
    protected final TreeMap<Integer, List<SimpleConsumer<T>>> _postDeleteConsumer = new TreeMap<>();
    protected final TreeMap<Integer, List<SimpleConsumer<DeleteRecordWrapper<T>>>> _postWrapperDeleteConsumer = new TreeMap<>();
    protected final TreeMap<Integer, List<SimpleResultConsumer<T>>> _afterDeleteConsumer = new TreeMap<>();
    protected final TreeMap<Integer, List<SimpleResultConsumer<DeleteRecordWrapper<T>>>> _afterWrapperDeleteConsumer = new TreeMap<>();


    public _SimpleServerImpl(_SimplePersistence<T> persistence){
        this._persistence = persistence;
        this._class = this.getClass();
        this._bean = this;
        try {
            registerProcessor();
        }catch (Throwable e){
            log.error("扫描各种处理器异常：", e);
        }
    }

    @Override
    public String name() {
        return _persistence.name();
    }
    @Override
    public Class<T> domainClass() {
        return _persistence.domainClass();
    }
    @Override
    @SuppressWarnings("unchecked")
    public <P extends _SimplePersistence<T>> P persistence(){
        return (P)_persistence;
    }

    protected void registerPreWrapperAddConsumer(SimpleConsumer<AddRecordWrapper<T>> consumer, int order){
        _preWrapperAddConsumer.computeIfAbsent(order, k -> new ArrayList<>()).add(consumer);
    }
    protected void registerPreAddConsumer(SimpleConsumer<T> consumer, int order){
        _preAddConsumer.computeIfAbsent(order, k -> new ArrayList<>()).add(consumer);
    }
    protected void registerPostWrapperAddConsumer(SimpleConsumer<AddRecordWrapper<T>> consumer, int order){
        _postWrapperAddConsumer.computeIfAbsent(order, k -> new ArrayList<>()).add(consumer);
    }
    protected void registerPostAddConsumer(SimpleConsumer<T> consumer, int order){
        _postAddConsumer.computeIfAbsent(order, k -> new ArrayList<>()).add(consumer);
    }
    protected void registerAfterAddConsumer(SimpleResultConsumer<T> consumer, int order){
        _afterAddConsumer.computeIfAbsent(order, k -> new ArrayList<>()).add(consumer);
    }
    protected void registerAfterWrapperAddConsumer(SimpleResultConsumer<AddRecordWrapper<T>> consumer, int order){
        _afterWrapperAddConsumer.computeIfAbsent(order, k -> new ArrayList<>()).add(consumer);
    }
    protected void registerPreWrapperEditConsumer(SimpleConsumer<EditRecordWrapper<T>> consumer, int order){
        _preWrapperEditConsumer.computeIfAbsent(order, k -> new ArrayList<>()).add(consumer);
    }
    protected void registerPreEditConsumer(SimpleConsumer2<T> consumer, int order){
        _preEditConsumer.computeIfAbsent(order, k -> new ArrayList<>()).add(consumer);
    }
    protected void registerPostWrapperEditConsumer(SimpleConsumer<EditRecordWrapper<T>> consumer, int order){
        _postWrapperEditConsumer.computeIfAbsent(order, k -> new ArrayList<>()).add(consumer);
    }
    protected void registerPostEditConsumer(SimpleConsumer2<T> consumer, int order){
        _postEditConsumer.computeIfAbsent(order, k -> new ArrayList<>()).add(consumer);
    }
    protected void registerAfterEditConsumer(SimpleResultConsumer2<T> consumer, int order){
        _afterEditConsumer.computeIfAbsent(order, k -> new ArrayList<>()).add(consumer);
    }
    protected void registerAfterWrapperEditConsumer(SimpleResultConsumer<EditRecordWrapper<T>> consumer, int order){
        _afterWrapperEditConsumer.computeIfAbsent(order, k -> new ArrayList<>()).add(consumer);
    }
    protected void registerPreWrapperDeleteConsumer(SimpleConsumer<DeleteRecordWrapper<T>> consumer, int order){
        _preWrapperDeleteConsumer.computeIfAbsent(order, k -> new ArrayList<>()).add(consumer);
    }
    protected void registerPreDeleteConsumer(SimpleConsumer<T> consumer, int order){
        _preDeleteConsumer.computeIfAbsent(order, k -> new ArrayList<>()).add(consumer);
    }
    protected void registerPostWrapperDeleteConsumer(SimpleConsumer<DeleteRecordWrapper<T>> consumer, int order){
        _postWrapperDeleteConsumer.computeIfAbsent(order, k -> new ArrayList<>()).add(consumer);
    }
    protected void registerPostDeleteConsumer(SimpleConsumer<T> consumer, int order){
        _postDeleteConsumer.computeIfAbsent(order, k -> new ArrayList<>()).add(consumer);
    }
    protected void registerAfterDeleteConsumer(SimpleResultConsumer<T> consumer, int order){
        _afterDeleteConsumer.computeIfAbsent(order, k -> new ArrayList<>()).add(consumer);
    }
    protected void registerAfterWrapperDeleteConsumer(SimpleResultConsumer<DeleteRecordWrapper<T>> consumer, int order){
        _afterWrapperDeleteConsumer.computeIfAbsent(order, k -> new ArrayList<>()).add(consumer);
    }

    @Override
    public void preAdd(T record) throws Exception{
        for (Map.Entry<Integer, List<SimpleConsumer<T>>> entry : _preAddConsumer.entrySet()){
            for (SimpleConsumer<T> consumer : entry.getValue()){
                try {
                    consumer.accept(record);
                }catch (Exception e){
                    Logger log = org.slf4j.LoggerFactory.getLogger(_SimpleServerImpl.class);
                    log.info("新增简单对象：{}，前置处理失败：", name(), e);
                    afterAdd(record, false);
                    throw e;
                }
            }
        }
    }
    @Override
    public void preAdd(AddRecordWrapper<T> wrapper) throws Exception{
        for (Map.Entry<Integer, List<SimpleConsumer<AddRecordWrapper<T>>>> entry : _preWrapperAddConsumer.entrySet()) {
            for (SimpleConsumer<AddRecordWrapper<T>> consumer : entry.getValue()) {
                try {
                    consumer.accept(wrapper);
                } catch (Exception e) {
                    Logger log = org.slf4j.LoggerFactory.getLogger(_SimpleServerImpl.class);
                    log.info("新增简单对象：{}，前置处理失败：", name(), e);
                    afterAdd(wrapper, false);
                    throw e;
                }
            }
        }
    }
    protected void postAdd(T record) throws Exception{
        for (Map.Entry<Integer, List<SimpleConsumer<T>>> entry : _postAddConsumer.entrySet()) {
            for (SimpleConsumer<T> consumer : entry.getValue()) {
                consumer.accept(record);
            }
        }
    }
    protected void postAdd(AddRecordWrapper<T> wrapper) throws Exception{
        for (Map.Entry<Integer, List<SimpleConsumer<AddRecordWrapper<T>>>> entry : _postWrapperAddConsumer.entrySet()) {
            for (SimpleConsumer<AddRecordWrapper<T>> consumer : entry.getValue()) {
                consumer.accept(wrapper);
            }
        }
    }
    protected void afterAdd(T record, boolean result){
        for (Map.Entry<Integer, List<SimpleResultConsumer<T>>> entry : _afterAddConsumer.entrySet()) {
            for (SimpleResultConsumer<T> consumer : entry.getValue()) {
                consumer.accept(record, result);
            }
        }
    }
    protected void afterAdd(AddRecordWrapper<T> record, boolean result){
        for (Map.Entry<Integer, List<SimpleResultConsumer<AddRecordWrapper<T>>>> entry : _afterWrapperAddConsumer.entrySet()) {
            for (SimpleResultConsumer<AddRecordWrapper<T>> consumer : entry.getValue()) {
                consumer.accept(record, result);
            }
        }
    }
    @Override
    public T add(T record) throws Exception{
        boolean succeed = false;
        try{
            _persistence.save(record);
            postAdd(record);
            succeed = true;
            return record;
        }catch (Throwable e) {
            Logger log = org.slf4j.LoggerFactory.getLogger(_SimpleServerImpl.class);
            log.warn("新增简单对象：{} 失败：", name(), e);
            Throwable cause = e.getCause();
            if (cause instanceof Exception){
                throw (Exception)cause;
            }
            throw new AddObjectException(record.getClass(), name(), e);
        }finally {
            afterAdd(record, succeed);
        }
    }
    @Override
    public void add(AddRecordWrapper<T> wrapper) throws Exception{
        boolean succeed = false;
        try{
            _persistence.save(wrapper.getRecord());
            postAdd(wrapper);
            succeed = true;
        }catch (Throwable e) {
            Logger log = org.slf4j.LoggerFactory.getLogger(_SimpleServerImpl.class);
            log.warn("新增简单对象：{} 失败：", name(), e);
            Throwable cause = e.getCause();
            if (cause instanceof Exception){
                throw (Exception)cause;
            }
            throw new AddObjectException(domainClass(), name(), e);
        }finally {
            afterAdd(wrapper, succeed);
        }
    }

    @Override
    public void preEdit(EditRecordWrapper<T> wrapper) throws Exception{
        for (Map.Entry<Integer, List<SimpleConsumer<EditRecordWrapper<T>>>> entry : _preWrapperEditConsumer.entrySet()) {
            for (SimpleConsumer<EditRecordWrapper<T>> consumer : entry.getValue()) {
                try {
                    consumer.accept(wrapper);
                } catch (Exception e) {
                    Logger log = org.slf4j.LoggerFactory.getLogger(_SimpleServerImpl.class);
                    log.info("编辑简单对象：{}，前置处理失败：", name(), e);
                    afterEdit(wrapper, false);
                    throw e;
                }
            }
        }
    }
    @Override
    public void preEdit(T original, T record) throws Exception{
        for (Map.Entry<Integer, List<SimpleConsumer2<T>>> entry : _preEditConsumer.entrySet()) {
            for (SimpleConsumer2<T> consumer : entry.getValue()) {
                try {
                    consumer.accept(original, record);
                } catch (Exception e) {
                    Logger log = org.slf4j.LoggerFactory.getLogger(_SimpleServerImpl.class);
                    log.info("编辑简单对象：{}，前置处理失败：", name(), e);
                    afterEdit(original, record, false);
                    throw e;
                }
            }
        }
    }
    protected void afterEdit(T original, T record, boolean result){
        for (Map.Entry<Integer, List<SimpleResultConsumer2<T>>> entry : _afterEditConsumer.entrySet()) {
            for (SimpleResultConsumer2<T> consumer : entry.getValue()) {
                consumer.accept(original, record, result);
            }
        }
    }
    protected void afterEdit(EditRecordWrapper<T> wrapper, boolean result){
        for (Map.Entry<Integer, List<SimpleResultConsumer<EditRecordWrapper<T>>>> entry : _afterWrapperEditConsumer.entrySet()) {
            for (SimpleResultConsumer<EditRecordWrapper<T>> consumer : entry.getValue()) {
                consumer.accept(wrapper, result);
            }
        }
    }
    protected void postEdit(T original, T record) throws Exception{
        for (Map.Entry<Integer, List<SimpleConsumer2<T>>> entry : _postEditConsumer.entrySet()) {
            for (SimpleConsumer2<T> consumer : entry.getValue()) {
                consumer.accept(original, record);
            }
        }
    }
    protected void postEdit(EditRecordWrapper<T> wrapper) throws Exception{
        for (Map.Entry<Integer, List<SimpleConsumer<EditRecordWrapper<T>>>> entry : _postWrapperEditConsumer.entrySet()) {
            for (SimpleConsumer<EditRecordWrapper<T>> consumer : entry.getValue()) {
                consumer.accept(wrapper);
            }
        }
    }

    @Override
    public T edit(T original, T record) throws Exception{
        boolean succeed = false;
        try{
            _persistence.updateById(record);
            postEdit(original, record);
            succeed = true;
            return record;
        }catch (Exception e) {
            Logger log = org.slf4j.LoggerFactory.getLogger(_SimpleServerImpl.class);
            log.warn("修改简单对象：{} 失败：", name(), e);
            Throwable cause = e.getCause();
            if (cause != null){
                if (cause instanceof Exception) {
                    throw (Exception)cause;
                }
            }
            throw new ModifyObjectException(record.getClass(), name(), e);
        }finally {
            afterEdit(original, record, succeed);
        }
    }
    @Override
    public void edit(EditRecordWrapper<T> wrapper) throws Exception{
        boolean succeed = false;
        try{
            _persistence.updateById(wrapper.getRecord());
            postEdit(wrapper);

            succeed = true;
        }catch (Exception e) {
            Logger log = org.slf4j.LoggerFactory.getLogger(_SimpleServerImpl.class);
            log.warn("修改简单对象：{} 失败：", name(), e);
            Throwable cause = e.getCause();
            if (cause != null){
                if (cause instanceof Exception) {
                    throw (Exception)cause;
                }
            }
            throw new ModifyObjectException(domainClass(), name(), e);
        }finally {
            afterEdit(wrapper, succeed);
        }
    }

    @Override
    public void preDelete(T record) throws Exception{
        for (Map.Entry<Integer, List<SimpleConsumer<T>>> entry : _preDeleteConsumer.entrySet()) {
            for (SimpleConsumer<T> consumer : entry.getValue()) {
                try {
                    consumer.accept(record);
                } catch (Exception e) {
                    Logger log = org.slf4j.LoggerFactory.getLogger(_SimpleServerImpl.class);
                    log.info("删除简单对象：{}，前置处理失败：", name(), e);
                    afterDelete(record, false);
                    throw e;
                }
            }
        }
    }
    @Override
    public void preDelete(DeleteRecordWrapper<T> wrapper) throws Exception{
        for (Map.Entry<Integer, List<SimpleConsumer<DeleteRecordWrapper<T>>>> entry : _preWrapperDeleteConsumer.entrySet()) {
            for (SimpleConsumer<DeleteRecordWrapper<T>> consumer : entry.getValue()) {
                try {
                    consumer.accept(wrapper);
                } catch (Exception e) {
                    Logger log = org.slf4j.LoggerFactory.getLogger(_SimpleServerImpl.class);
                    log.info("删除简单对象：{}，前置处理失败：", name(), e);
                    afterDelete(wrapper, false);
                    throw e;
                }
            }
        }
    }
    protected void postDelete(T record) throws Exception{
        for (Map.Entry<Integer, List<SimpleConsumer<T>>> entry : _postDeleteConsumer.entrySet()) {
            for (SimpleConsumer<T> consumer : entry.getValue()) {
                consumer.accept(record);
            }
        }
    }
    protected void postDelete(DeleteRecordWrapper<T> wrapper) throws Exception{
        for (Map.Entry<Integer, List<SimpleConsumer<DeleteRecordWrapper<T>>>> entry : _postWrapperDeleteConsumer.entrySet()) {
            for (SimpleConsumer<DeleteRecordWrapper<T>> consumer : entry.getValue()) {
                consumer.accept(wrapper);
            }
        }
    }
    protected void afterDelete(T record, boolean result){
        for (Map.Entry<Integer, List<SimpleResultConsumer<T>>> entry : _afterDeleteConsumer.entrySet()) {
            for (SimpleResultConsumer<T> consumer : entry.getValue()) {
                consumer.accept(record, result);
            }
        }
    }
    protected void afterDelete(DeleteRecordWrapper<T> record, boolean result){
        for (Map.Entry<Integer, List<SimpleResultConsumer<DeleteRecordWrapper<T>>>> entry : _afterWrapperDeleteConsumer.entrySet()) {
            for (SimpleResultConsumer<DeleteRecordWrapper<T>> consumer : entry.getValue()) {
                consumer.accept(record, result);
            }
        }
    }

    @Override
    public T delete(T record) throws Exception{
        boolean succeed = false;
        try{
            _persistence.removeById(record);
            postDelete(record);
            succeed = true;
            return record;
        }catch (Throwable e) {
            Logger log = org.slf4j.LoggerFactory.getLogger(_SimpleServerImpl.class);
            log.warn("删除简单对象：{} 失败：", name(), e);
            throw e;
        }finally {
            afterDelete(record, succeed);
        }
    }

    @Override
    public void delete(DeleteRecordWrapper<T> wrapper) throws Exception{
        boolean succeed = false;
        try{
            _persistence.removeById(wrapper.getRecord().getId());
            postDelete(wrapper);
            succeed = true;
        }catch (Throwable e) {
            Logger log = org.slf4j.LoggerFactory.getLogger(_SimpleServerImpl.class);
            log.warn("删除简单对象：{} 失败：", name(), e);
            throw e;
        }finally {
            afterDelete(wrapper, succeed);
        }
    }

    @Override
    public ListWrapper<T> query(Q<T> input, Integer pageNo, Integer pageSize){
        QueryWrapper<T> wrapper = new QueryWrapper<>();
        wrapper.setEntityClass(domainClass());
        // 构建查询条件
        input.build(wrapper);

        if (pageNo == null){
            pageNo = 1;
        }
        if (pageSize == null){
            pageSize = 50;
        }
        if (pageSize < 1 || pageNo < 1) {
            // 不分页查询
            List<T> records = _persistence.list(wrapper);
            return new ListWrapper<>(records);
        }
        // 分页，并根据条件确定是否获取记录总数
        IPage<T> page = new Page<>(pageNo, pageSize, input.isSearchCount());
        IPage<T> records = _persistence.page(page, wrapper);
        return new ListWrapper<>((int)records.getTotal(), records.getRecords());
    }

    @Override
    public T getById(Serializable id){
        return _persistence.getById(id);
    }

    protected void registerProcessor() throws Throwable{
        // 扫码本bean的各种处理器
        Method[] methods = this._class.getDeclaredMethods();
        for (Method method : methods){
            if (method.isAnnotationPresent(AddPreProcessor.class)){
                // 这是新增前置处理器
                AddPreProcessor processor = method.getAnnotation(AddPreProcessor.class);
                // 获取参数类型
                Class<?>[] parameterTypes = method.getParameterTypes();
                // 对于add前置，仅支持一个参数
                if (parameterTypes.length != 1){
                    log.warn("标记为新增前置处理的方法：{}，参数签名不正确，新增前置处理器仅支持一个参数", method.getName());
                    throw new RuntimeException("新增前置处理器的参数数量不正确");
                }
                if (AddRecordWrapper.class.isAssignableFrom(parameterTypes[0])){
                    SimpleConsumer<AddRecordWrapper<T>> consumer = FunctionalTool.simpleConsumer(method, this._bean);
                    registerPreWrapperAddConsumer(consumer, processor.order());
                }else if (domainClass().equals(parameterTypes[0])){
                    SimpleConsumer<T> consumer = FunctionalTool.simpleConsumer(method, this._bean);
                    registerPreAddConsumer(consumer, processor.order());
                }else{
                    log.warn("标记为新增前置处理的方法：{}，参数类型不正确", method.getName());
                    throw new RuntimeException("新增前置处理器的参数类型不正确");
                }
            }else if (method.isAnnotationPresent(AddPostProcessor.class )){
                // 后置处理器
                AddPostProcessor processor = method.getAnnotation(AddPostProcessor.class);
                // 获取参数类型
                Class<?>[] parameterTypes = method.getParameterTypes();
                // 对于add前置，仅支持一个参数
                if (parameterTypes.length != 1){
                    log.warn("标记为新增后置处理的方法：{}，参数签名不正确，新增后置处理器仅支持一个参数", method.getName());
                    throw new RuntimeException("新增后置处理器的参数数量不正确");
                }
                if (AddRecordWrapper.class.isAssignableFrom(parameterTypes[0])){
                    SimpleConsumer<AddRecordWrapper<T>> consumer = FunctionalTool.simpleConsumer(method, this._bean);
                    registerPostWrapperAddConsumer(consumer, processor.order());
                }else if (domainClass().equals(parameterTypes[0])){
                    SimpleConsumer<T> consumer = FunctionalTool.simpleConsumer(method, this._bean);
                    registerPostAddConsumer(consumer, processor.order());
                }else{
                    log.warn("标记为新增后置处理的方法：{}，参数类型不正确", method.getName());
                    throw new RuntimeException("新增后置处理器的参数类型不正确");
                }
            }else if (method.isAnnotationPresent(AddAfterProcessor.class )){
                // 收尾处理器
                AddAfterProcessor processor = method.getAnnotation(AddAfterProcessor.class);
                // 获取参数类型
                Class<?>[] parameterTypes = method.getParameterTypes();
                // 对于add收尾处理，仅支持一个参数
                if (parameterTypes.length != 2){
                    log.warn("标记为新增收尾处理的方法：{}，参数签名不正确，新增收尾处理器仅支持两个参数", method.getName());
                    throw new RuntimeException("新增收尾处理器的参数数量不正确");
                }
                if (AddRecordWrapper.class.isAssignableFrom(parameterTypes[0])){
                    SimpleResultConsumer<AddRecordWrapper<T>> consumer = FunctionalTool.simpleResultConsumer(method, this._bean);
                    registerAfterWrapperAddConsumer(consumer, processor.order());
                }else if (domainClass().equals(parameterTypes[0])){
                    SimpleResultConsumer<T> consumer = FunctionalTool.simpleResultConsumer(method, this._bean);
                    registerAfterAddConsumer(consumer, processor.order());
                }else{
                    log.warn("标记为新增收尾处理的方法：{}，参数类型不正确", method.getName());
                    throw new RuntimeException("新增收尾处理器的参数类型不正确");
                }
            }else if (method.isAnnotationPresent(EditPreProcessor.class)){
                // 编辑前置处理器
                EditPreProcessor processor = method.getAnnotation(EditPreProcessor.class);
                // 获取参数类型
                Class<?>[] parameterTypes = method.getParameterTypes();
                if (parameterTypes.length == 1){
                    if (EditRecordWrapper.class.isAssignableFrom(parameterTypes[0])){
                        SimpleConsumer<EditRecordWrapper<T>> consumer = FunctionalTool.simpleConsumer(method, this._bean);
                        registerPreWrapperEditConsumer(consumer, processor.order());
                    }else{
                        log.warn("标记为编辑前置处理的方法：{}，参数类型不正确", method.getName());
                        throw new RuntimeException("编辑前置处理器的参数类型不正确");
                    }
                }else if (parameterTypes.length == 2){
                    // 2个参数
                    if (domainClass().equals(parameterTypes[0]) && domainClass().equals(parameterTypes[1])){
                        SimpleConsumer2<T> consumer = FunctionalTool.simpleConsumer2(method, this._bean);
                        registerPreEditConsumer(consumer, processor.order());
                    }else{
                        log.warn("标记为编辑前置处理的方法：{}，参数类型不正确", method.getName());
                        throw new RuntimeException("编辑前置处理器的参数类型不正确");
                    }
                }
            }else if (method.isAnnotationPresent(EditPostProcessor.class)){
                // 编辑后置处理器
                EditPostProcessor processor = method.getAnnotation(EditPostProcessor.class);
                // 获取参数类型
                Class<?>[] parameterTypes = method.getParameterTypes();
                if (parameterTypes.length == 1){
                    if (EditRecordWrapper.class.isAssignableFrom(parameterTypes[0])){
                        SimpleConsumer<EditRecordWrapper<T>> consumer = FunctionalTool.simpleConsumer(method, this._bean);
                        registerPostWrapperEditConsumer(consumer, processor.order());
                    }else{
                        log.warn("标记为编辑后置处理的方法：{}，参数类型不正确", method.getName());
                        throw new RuntimeException("编辑后置处理器的参数类型不正确");
                    }
                }else if (parameterTypes.length == 2){
                    // 2个参数
                    if (domainClass().equals(parameterTypes[0]) && domainClass().equals(parameterTypes[1])){
                        SimpleConsumer2<T> consumer = FunctionalTool.simpleConsumer2(method, this._bean);
                        registerPostEditConsumer(consumer, processor.order());
                    }else{
                        log.warn("标记为编辑后置处理的方法：{}，参数类型不正确", method.getName());
                        throw new RuntimeException("编辑后置处理器的参数类型不正确");
                    }
                }
            }else if (method.isAnnotationPresent(EditAfterProcessor.class)){
                // 编辑后置处理器
                EditAfterProcessor processor = method.getAnnotation(EditAfterProcessor.class);
                // 获取参数类型
                Class<?>[] parameterTypes = method.getParameterTypes();
                if (parameterTypes.length == 2){
                    if (EditRecordWrapper.class.isAssignableFrom(parameterTypes[0]) && boolean.class.equals(parameterTypes[1])){
                        SimpleResultConsumer<EditRecordWrapper<T>> consumer = FunctionalTool.simpleResultConsumer(method, this._bean);
                        registerAfterWrapperEditConsumer(consumer, processor.order());
                    }else{
                        log.warn("标记为编辑后置处理的方法：{}，参数类型不正确", method.getName());
                        throw new RuntimeException("编辑后置处理器的参数类型不正确");
                    }
                }else if (parameterTypes.length == 3){
                    // 3个参数
                    if (domainClass().equals(parameterTypes[0]) && domainClass().equals(parameterTypes[1]) && boolean.class.equals(parameterTypes[2])){
                        SimpleResultConsumer2<T> consumer = FunctionalTool.simpleResultConsumer2(method, this._bean);
                        registerAfterEditConsumer(consumer, processor.order());
                    }else{
                        log.warn("标记为编辑后置处理的方法：{}，参数类型不正确", method.getName());
                        throw new RuntimeException("编辑后置处理器的参数类型不正确");
                    }
                }
            }else if (method.isAnnotationPresent(DeletePreProcessor.class)){
                // 这是删除前置处理器
                DeletePreProcessor processor = method.getAnnotation(DeletePreProcessor.class);
                // 获取参数类型
                Class<?>[] parameterTypes = method.getParameterTypes();
                // 对于delete前置，仅支持一个参数
                if (parameterTypes.length != 1){
                    log.warn("标记为删除前置处理的方法：{}，参数签名不正确，删除前置处理器仅支持一个参数", method.getName());
                    throw new RuntimeException("删除前置处理器的参数数量不正确");
                }
                if (DeleteRecordWrapper.class.isAssignableFrom(parameterTypes[0])){
                    SimpleConsumer<DeleteRecordWrapper<T>> consumer = FunctionalTool.simpleConsumer(method, this._bean);
                    registerPreWrapperDeleteConsumer(consumer, processor.order());
                }else if (domainClass().equals(parameterTypes[0])){
                    SimpleConsumer<T> consumer = FunctionalTool.simpleConsumer(method, this._bean);
                    registerPreDeleteConsumer(consumer, processor.order());
                }else{
                    log.warn("标记为删除前置处理的方法：{}，参数类型不正确", method.getName());
                    throw new RuntimeException("删除前置处理器的参数类型不正确");
                }
            }else if (method.isAnnotationPresent(DeletePostProcessor.class )){
                // 后置处理器
                DeletePostProcessor processor = method.getAnnotation(DeletePostProcessor.class);
                // 获取参数类型
                Class<?>[] parameterTypes = method.getParameterTypes();
                // 对于delete前置，仅支持一个参数
                if (parameterTypes.length != 1){
                    log.warn("标记为删除后置处理的方法：{}，参数签名不正确，删除后置处理器仅支持一个参数", method.getName());
                    throw new RuntimeException("删除后置处理器的参数数量不正确");
                }
                if (DeleteRecordWrapper.class.isAssignableFrom(parameterTypes[0])){
                    SimpleConsumer<DeleteRecordWrapper<T>> consumer = FunctionalTool.simpleConsumer(method, this._bean);
                    registerPostWrapperDeleteConsumer(consumer, processor.order());
                }else if (domainClass().equals(parameterTypes[0])){
                    SimpleConsumer<T> consumer = FunctionalTool.simpleConsumer(method, this._bean);
                    registerPostDeleteConsumer(consumer, processor.order());
                }else{
                    log.warn("标记为删除后置处理的方法：{}，参数类型不正确", method.getName());
                    throw new RuntimeException("删除后置处理器的参数类型不正确");
                }
            }else if (method.isAnnotationPresent(DeleteAfterProcessor.class )){
                // 收尾处理器
                DeleteAfterProcessor processor = method.getAnnotation(DeleteAfterProcessor.class);
                // 获取参数类型
                Class<?>[] parameterTypes = method.getParameterTypes();
                // 对于delete收尾处理，仅支持一个参数
                if (parameterTypes.length != 2){
                    log.warn("标记为删除收尾处理的方法：{}，参数签名不正确，删除收尾处理器仅支持两个参数", method.getName());
                    throw new RuntimeException("删除收尾处理器的参数数量不正确");
                }
                if (DeleteRecordWrapper.class.isAssignableFrom(parameterTypes[0])){
                    SimpleResultConsumer<DeleteRecordWrapper<T>> consumer = FunctionalTool.simpleResultConsumer(method, this._bean);
                    registerAfterWrapperDeleteConsumer(consumer, processor.order());
                }else if (domainClass().equals(parameterTypes[0])){
                    SimpleResultConsumer<T> consumer = FunctionalTool.simpleResultConsumer(method, this._bean);
                    registerAfterDeleteConsumer(consumer, processor.order());
                }else{
                    log.warn("标记为删除收尾处理的方法：{}，参数类型不正确", method.getName());
                    throw new RuntimeException("删除收尾处理器的参数类型不正确");
                }
            }
        }
    }
}
